package com.hx.mypetstore.ui.goods;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.hx.mypetstore.Bean.GoodsBean;
import com.hx.mypetstore.R;
import com.hx.mypetstore.constant.SPConstants;
import com.hx.mypetstore.network.HttpURL;
import com.hx.mypetstore.ui.Details.DetailsActivity;
import com.hx.mypetstore.util.SPUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GoodsActivity extends AppCompatActivity implements View.OnClickListener{

    /**
     * 刷新的layout
     */
    private SwipeRefreshLayout mRefreshLayout;
    /**
     * 商品list
     */
    private ListView mListView;

    /**
     * 商品数据
     * @param savedInstanceState
     */

    /**
     * 适配器
     */
    private GoodsAdapter mAdapter;

    private List<GoodsBean> goods;
    private String TAG = "商品页";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        init();
    }

    private void init() {
        mListView = findViewById(R.id.dataList);
        mRefreshLayout = findViewById(R.id.refresh_layout);
        initData();
        goods = SPUtil.getDataList(this,SPConstants.GOODS_LIST,GoodsBean.class);
        Log.e(TAG, "init: "+goods );
        mAdapter = new GoodsAdapter(this,goods);
        mListView.setAdapter(mAdapter);
        findViewById(R.id.title_back).setOnClickListener(this);
        //设置列表Item点击监听事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                GoodsBean model = SPUtil.getDataList(getApplicationContext(),SPConstants.GOODS_LIST,GoodsBean.class).get(i);
                Intent intent = new Intent(GoodsActivity.this, DetailsActivity.class);
                intent.putExtra("id", i + "");
//                Log.e(TAG, "onItemClick: "+model.getId() );
                startActivity(intent);
            }
        });
    }

    private void initData() {
        String url = HttpURL.ALLGOODS;
        //发送注册请求
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().string());
                    Log.e(TAG, "商品数据"+jsonObject.toString());
                    if(!jsonObject.getBoolean("success"))
                        ;
                    else{
                        String good = jsonObject.getString("data");
                        //Json 解析对象
                        JsonParser jsonParser = new JsonParser();
                        JsonArray jsonArray = jsonParser.parse(good).getAsJsonArray();
                        Log.e(TAG, "JsonArray"+jsonArray );
                        //JsonArray jsonArray = jsonObject1.getAsJsonArray("msg");
                        Gson gson = new Gson();
                        ArrayList<GoodsBean> list = new ArrayList<>();
                        for (JsonElement add : jsonArray) {
                            GoodsBean goodsBean = gson.fromJson(add, GoodsBean.class);
                            Log.e(TAG, "Bean"+goodsBean );
                            list.add(goodsBean);
                        }
                        Log.e(TAG, "goods"+list);
                        SPUtil.saveDataList(getApplication(), SPConstants.GOODS_LIST,list);
                        Log.e(TAG, "成功存"+SPUtil.getDataList(getApplication(),SPConstants.GOODS_LIST,GoodsBean.class) );
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_back:
                finish();
        }
    }
}